package com.example.ssrbac.controller;

import com.example.ssrbac.dto.PageDTO;
import com.example.ssrbac.service.PageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 页面控制器
 * 
 * 该控制器负责处理页面管理相关的请求，包括：
 * 1. 创建页面
 * 2. 更新页面信息
 * 3. 删除页面
 * 4. 查询页面信息
 * 5. 获取页面树形结构
 * 
 * @author Your Name
 * @version 1.0
 */
@RestController
@RequestMapping("/api/pages")
public class PageController {
    private static final Logger logger = LoggerFactory.getLogger(PageController.class);

    /**
     * 页面服务，用于处理页面相关的业务逻辑
     */
    @Autowired
    private PageService pageService;

    /**
     * 创建新页面
     * 需要PAGE_CREATE权限
     * 
     * @param pageDTO 页面信息
     * @return 创建的页面信息
     */
    @PostMapping
    @PreAuthorize("hasAuthority('PAGE_CREATE')")
    public ResponseEntity<PageDTO> createPage(@Valid @RequestBody PageDTO pageDTO) {
        logger.info("Creating new page with name: {}", pageDTO.getName());
        PageDTO createdPage = pageService.createPage(pageDTO);
        logger.info("Page created successfully with ID: {}", createdPage.getId());
        return ResponseEntity.ok(createdPage);
    }

    /**
     * 更新页面信息
     * 需要PAGE_UPDATE权限
     * 
     * @param id 页面ID
     * @param pageDTO 更新的页面信息
     * @return 更新后的页面信息
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('PAGE_UPDATE')")
    public ResponseEntity<PageDTO> updatePage(@PathVariable Long id, @Valid @RequestBody PageDTO pageDTO) {
        logger.info("Updating page with ID: {}", id);
        PageDTO updatedPage = pageService.updatePage(id, pageDTO);
        logger.info("Page updated successfully: {}", id);
        return ResponseEntity.ok(updatedPage);
    }

    /**
     * 删除页面
     * 需要PAGE_DELETE权限
     * 
     * @param id 页面ID
     * @return 空响应
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('PAGE_DELETE')")
    public ResponseEntity<Void> deletePage(@PathVariable Long id) {
        logger.info("Deleting page with ID: {}", id);
        pageService.deletePage(id);
        logger.info("Page deleted successfully: {}", id);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据ID查询页面
     * 需要PAGE_READ权限
     * 
     * @param id 页面ID
     * @return 页面信息
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('PAGE_READ')")
    public ResponseEntity<PageDTO> getPageById(@PathVariable Long id) {
        logger.debug("Fetching page by ID: {}", id);
        PageDTO page = pageService.getPageById(id);
        logger.debug("Page fetched successfully: {}", id);
        return ResponseEntity.ok(page);
    }

    /**
     * 分页查询所有页面
     * 需要PAGE_READ权限
     * 
     * @param pageable 分页参数
     * @return 页面信息分页列表
     */
    @GetMapping
    @PreAuthorize("hasAuthority('PAGE_READ')")
    public ResponseEntity<Page<PageDTO>> getAllPages(Pageable pageable) {
        logger.debug("Fetching all pages with pagination: {}", pageable);
        Page<PageDTO> pages = pageService.getAllPages(pageable);
        logger.debug("All pages fetched successfully, total: {}", pages.getTotalElements());
        return ResponseEntity.ok(pages);
    }

    /**
     * 获取页面树形结构
     * 需要PAGE_READ权限
     * 
     * @return 页面树形结构
     */
    @GetMapping("/tree")
    @PreAuthorize("hasAuthority('PAGE_READ')")
    public ResponseEntity<PageDTO> getPageTree() {
        logger.debug("Fetching page tree structure");
        PageDTO pageTree = pageService.getPageTree();
        logger.debug("Page tree fetched successfully");
        return ResponseEntity.ok(pageTree);
    }
} 